package com.jerry.usbserial

import android.content.Context
import android.os.Environment
import android.util.Log
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.jerry.usbserial.util.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File
import java.io.FileInputStream
import kotlin.math.ceil

/**
 * Created by dugang on 2020/6/17.
 */
@RunWith(AndroidJUnit4::class)
class UsbFirmwareTaskTest {
    private val IMAGE_SLICE_SIZE = 512
    private lateinit var imageFile: File
    private lateinit var firmwareFile: File
    private var mImageFileLength: Long = 0
    private var mImageSlice: Int = 0
    private val TAG = "UpdateTaskTest"

    @Before
    fun before() {
        val context: Context = ApplicationProvider.getApplicationContext()
        imageFile = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "uhcflash.bin")
        mImageFileLength = imageFile.length()
        mImageSlice = ceil(mImageFileLength / IMAGE_SLICE_SIZE.toFloat()).toInt()

        firmwareFile = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "uhc.bin")
    }

    @Test
    fun imageCrc() {
        val fileArray = readIO(FileInputStream(imageFile))
        val imageCrc = CRCUtil.getCrc32(fileArray)
        Log.d(TAG, "imageCrc: ${imageCrc.toHex()}")

    }

    @Test
    fun send() {
        val fis = FileInputStream(imageFile)
        for (i in 0 until 10) {
            // 最后一块
            val waitSendSize: Int = if (i == mImageSlice - 1) (mImageFileLength - i * IMAGE_SLICE_SIZE).toInt() else IMAGE_SLICE_SIZE
            var sendComplete = false
            var offset = 0
            while (!sendComplete) {
                val cmdList = mutableListOf<Byte>()
                when {
                    offset == 0 -> {
                        cmdList.addAll("6A".hexToByteArray().toList())
                        cmdList.addAll((i * IMAGE_SLICE_SIZE).to4LByteArray().toList())
                        cmdList.add((i % 2).toByte())
                        cmdList.add(57.toByte())
                        val dataArray = ByteArray(57)
                        fis.read(dataArray)
                        cmdList.addAll(dataArray.toList())
                        write(cmdList.to64ByteArray())
                        offset += dataArray.size
                    }
                    waitSendSize - offset > 62 -> {
                        cmdList.addAll("6B".hexToByteArray().toList())
                        cmdList.add(62.toByte())
                        val dataArray = ByteArray(62)
                        fis.read(dataArray)
                        cmdList.addAll(dataArray.toList())
                        write(cmdList.to64ByteArray())
                        offset += dataArray.size
                    }
                    else -> {
                        cmdList.addAll("6C".hexToByteArray().toList())
                        cmdList.add((waitSendSize - offset).toByte())
                        val dataArray = ByteArray(waitSendSize - offset)
                        fis.read(dataArray)
                        cmdList.addAll(dataArray.toList())
                        write(cmdList.to64ByteArray())
                        sendComplete = true
                    }

                }
            }
        }
        fis.close()
    }

    private fun write(byteArray: ByteArray) {
        Log.d(TAG, "write: ${byteArray.toHex().toUpperCase()}")
    }

}